package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Floor;
import com.service.FloorService;
import com.util.ConstUtils;
import com.vo.DataVO;
import com.vo.FloorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/24 - 9:50
 */
@RestController
@RequestMapping("/floor")
public class FloorController {
    @Autowired
    private FloorService floorService;

    /**
     * 查询楼层列表
     *
     * @param floorVo
     * @return
     */
    @RequestMapping("/list")
    public DataVO<Floor> findFloorList(FloorVo floorVo) {
        PageHelper.startPage(floorVo.getPage(), floorVo.getLimit());
        PageInfo<Floor> pageInfo = new PageInfo<Floor>(floorService.findFloorList(floorVo));
        return new DataVO<Floor>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 添加楼层
     *
     * @param floor
     * @return
     */
    @RequestMapping("/add")
    public Map<String, Object> addFloor(Floor floor) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (floorService.addFloor(floor) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "添加成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "添加失败，请重试！");
        }
        return maps;
    }

    /**
     * 修改楼层
     *
     * @param floor
     * @return
     */
    @RequestMapping("/update")
    public Map<String, Object> updateFloor(Floor floor) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (floorService.updateFloor(floor) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "修改成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "修改失败，请重试！");
        }
        return maps;
    }

    /**
     * 查询楼层集合
     *
     * @return
     */
    @RequestMapping("/findFloorList")
    public String findFloorByRedis() {
        return floorService.findFloorByRedis();
    }
}
